//引入axios 核心库
import axios from 'axios'
//引入queryString
import qsString from 'querystring'
let baseUrl = '/api'

//引入拦截器
//请求拦截
axios.interceptors.request.use(req => req)

//响应拦截
axios.interceptors.response.use(res => {
    return res.data
})

//封装请求方式
/* 
get 
url   接口地址
params  入参
*/
export const get = (url, params = {}) => {
    return new Promise((resolve, reject) => {
        axios.get(baseUrl+url, {
                params
            })
            .then(res => resolve(res))
            .catch(err => reject(err))
    })
}

/* 
post
url   接口地址
params  入参
isFile 是否有文件
*/
export const post = (url, params = {}, isFile = false) => {
    let data = {}
    if (isFile) {
        data = new FormData()
        for (let i in params) {
            data.append(i, params[i])
        }
    } else {
        data = qsString.stringify(params)
    }
    return new Promise((resolve, reject) => {
        axios.post(baseUrl+url, data)
            .then(res => resolve(res))
            .catch(err => reject(err))
    })
}

